# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
    scipen = 999,
    digits = 16,
    max.print = .Machine$integer.max,
    show.error.locations = TRUE,
    warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------
library(data.table)
library(ggplot2)
library(scales)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1L)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Read in all estimates

new_marriage <-
    readRDS(
        "~/estimation-output/wealth_effect_estimates_new_marriage.rds"
    )
new_marriage <-
    new_marriage[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]
new_marriage[, outcome_label := "New Marriage (Single Winners)"]

divorce <-
    readRDS(
        "~/estimation-output/wealth_effect_estimates_divorce.rds"
    )
divorce <-
    divorce[
        model == "reduced_form" & ref_event_time %in% c(-7:5)
    ]
divorce[, outcome_label := "Divorce (Married Winners)"]

family_formation_stability <-
    rbindlist(
        list(new_marriage, divorce),
        use.names = TRUE
    )

# Add omitted; recall for these outcomes it was -1
temp <-
    CJ(
        outcome_label = unique(family_formation_stability$outcome_label),
        model = "reduced_form",
        income_quartile = 5
    )
temp[, ref_event_time := -1]
temp[, estimate := 0]
temp[, cluster_se := 0]

family_formation_stability <-
    rbindlist(list(family_formation_stability, temp), use.names = TRUE)
temp <- NULL
rm(temp)

family_formation_stability[
    ,
    outcome_label :=
        factor(
            outcome_label,
            levels =
            c(
                "New Marriage (Single Winners)",
                "Divorce (Married Winners)"
            )
        )
]

# Produce Figure B.13
# Rate of new marriage formation and divorce (percentage points)
family_formation_stability[, estimate := estimate * 100]
family_formation_stability[, cluster_se := cluster_se * 100]

figure_b_13 <-
    ggplot(
        aes(
            x = ref_event_time,
            y = estimate,
            color = factor(outcome_label),
            fill = factor(outcome_label)
        ),
        data = family_formation_stability
    ) +
    geom_line() +
    geom_ribbon(
        aes(
            ymin = estimate - (1.64 * cluster_se),
            ymax = estimate + (1.64 * cluster_se)
        ),
        linetype = 0,
        alpha = 0.2,
        show.legend = FALSE
    ) +
    theme_bw(base_size = 13) +
    theme(panel.grid.minor = element_blank()) +
    scale_x_continuous(
        breaks = seq.int(from = -7L, to = 5L, by = 1L),
        expand = expansion(mult = c(0.0025, 0.0025))
    ) +
    scale_y_continuous(breaks = seq.int(from = -20, to = 25, by = 5) / 10) +
    labs(x = "Event Time (ℓ)", y = "Event Study Estimate (pp)") +
    theme(
        legend.title = element_blank(),
        legend.position = c(0.225, 0.918),
        legend.background = element_rect(fill = "white", color = "grey"),
        strip.text.x = element_blank(),
        strip.background = element_blank(),
        legend.key = element_rect(NA),
        legend.spacing.y = unit(2, "mm"),
        legend.key.height = unit(4, "mm"),
        legend.key.width = unit(4, "mm"),
        legend.margin = margin(t = 0, r = 0.1, b = 0.15, l = 0.1, unit = "cm")
    ) +
    scale_color_viridis_d() +
    scale_fill_viridis_d() +
    guides(color = guide_legend(ncol = 1, byrow = TRUE)) +
    coord_cartesian(ylim = c(-2, 2.5))

ggsave(
    plot = figure_b_13,
    filename = "~/paper/figures/figure-B.13.png",
    width = 6,
    height = 4,
    dpi = 600
)

ggsave(
    plot = figure_b_13,
    filename = "~/paper/figures/figure-B.13.tif",
    width = 6,
    height = 4,
    device = "tiff",
    dpi = 600
)

# Housekeeping
new_marriage <- NULL
divorce <- NULL
family_formation_stability <- NULL
figure_b_13 <- NULL
rm(new_marriage, divorce, family_formation_stability, figure_b_13)